#!/usr/bin/perl

use strict;
use Card;
use DefaultDeck;

#
# This game is called HighLow and goes through a single deck of cards.
# Cards from the deck are dealt to the user, one by one.
# The user is asked to predict whether the next card will be higher or lower.
# The user's score is how many correct predictions are made.
#

print "This game is called HighLow and goes through a single deck of cards.\n";
print "Cards from the deck are dealt to you, one by one.\n";
print "You need to predict whether the next card will be higher or lower.\n";
print "The game ends when you make a wrong prediction.\n";
print "Your score is how many correct consecutive predictions you make.\n";
my $score = play(); # Play the game and get the score.
print "\n";
print "Your score was $score!\n";

sub play {
	my $currentCard = new Card();
	my $nextCard = new Card();
	my $currentname;
	my $currentvalue;
	my $nextname;
	my $nextvalue;
	my $gameScore;
	my $guess;
	$gameScore = 0;
	my $deck = new DefaultDeck();
	$currentCard->setname($deck->draw()->getname());
	$currentname = $currentCard->getname();
	$currentvalue = $currentCard->getvalue();
	print "The first card is $currentname with value $currentvalue.\n";
	while ($deck->size()) { # Loop ends when there are no more cards or the user is wrong.
		print "Do you think the next card will be lower (0) or higher (1)?\n";
		$guess = <STDIN>;
		while ($guess*$guess != $guess) {
			print "Please respond with 0 or 1.\n";
			$guess = <STDIN>;
		}
		$nextCard->setname($deck->draw()->getname());
		$nextname = $nextCard->getname();
		$nextvalue = $nextCard->getvalue();
		print "The next card is $nextname with value $nextvalue.\n";
		if ($nextCard->getvalue() == $currentCard->getvalue()) {
			print "The value is the same as the previous card.\n";
			print "You lose on ties. Sorry!\n";
			last; # End the game.
		} elsif ($nextCard->getvalue() > $currentCard->getvalue()) {
			if ($guess == 1) {
				print "Your prediction was correct.\n";
				$gameScore++;
			} else {
				print "Your prediction was incorrect.\n";
				last; # End the game.
			}
		} else { # nextCards is lower
			if ($guess == 0) {
				print "Your prediction was correct.\n";
				$gameScore++;
			} else {
				print "Your prediction was incorrect.\n";
				last; # End the game.
			}
		}
		# To set up for the next iteration of the loop, the nextCard
              # becomes the currentCard, since the currentCard has to be
              # the card that the user sees, and the nextCard will be
              # set to the next card in the deck after the user makes
              # his prediction.
		$currentCard->setname($nextCard->getname());
		$currentCard->setvalue($nextCard->getvalue());
		$currentname = $currentCard->getname();
		$currentvalue = $currentCard->getvalue();
		print "The card is $currentname with value $currentvalue.\n";
	} # End of while loop
	print "\n";
	print "The game is over.\n";
	print "You made $gameScore correct predictions.\n";
	print "\n";
	$gameScore;
}